package com.example.demo.service;

import com.example.demo.mapper.SellerProductMapper;
import com.example.demo.vo.ProductVO;
import com.example.demo.model.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SellerProductService {
    
    @Autowired
    private SellerProductMapper sellerProductMapper;
    
    public List<ProductVO> getProducts(Integer sellerId, String status) {
        return sellerProductMapper.selectBySeller(sellerId, status);
    }
    
    public boolean updateStatus(Integer productId, String status, Integer sellerId) {
        // 验证商品所有权
        ProductVO product = sellerProductMapper.selectById(productId);
        if (product == null || !product.getSellerId().equals(sellerId)) {
            return false;
        }
        return sellerProductMapper.updateStatus(productId, status) > 0;
    }
    
    public boolean delete(Integer productId, Integer sellerId) {
        // 验证商品所有权
        ProductVO product = sellerProductMapper.selectById(productId);
        if (product == null || !product.getSellerId().equals(sellerId)) {
            return false;
        }
        return sellerProductMapper.updateStatus(productId, "-1") > 0;
    }
    
    public ProductVO getProductDetail(Integer productId, Integer sellerId) {
        ProductVO product = sellerProductMapper.selectById(productId);
        // 验证商品所有权
        if (product == null || !product.getSellerId().equals(sellerId)) {
            return null;
        }
        return product;
    }
    
    /**
     * 更新商品信息
     */
    public boolean updateProduct(ProductInfo product) {
        try {
            return sellerProductMapper.update(product) > 0;
        } catch (Exception e) {
            log.error("更新商品失败", e);
            return false;
        }
    }
} 